package gov.pbc.nn.kjgl.maintenance.view;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.ParseResult;
import com.bstek.dorado.annotation.PropertyDef;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.dao.AbstractJdbcDao;
import gov.pbc.nn.kjgl.maintenance.entity.SysEmergencyScheme;

@Repository
public class SchemeJdbcDao extends AbstractJdbcDao {
	public void loadSys(Page<SysEmergencyScheme> page, Criteria criteria) {
		String sql = "SELECT\n" + "	T.*\n" + "FROM\n" + "	(\n" + "		SELECT\n"
				+ "			A.*, B.FILE_NAME_ AS YWXQS_\n" + "		FROM\n" + "			T_SYS_EMERGENCY_SCHEME AS A\n"
				+ "		LEFT JOIN T_SYS_MATERIAL AS B ON a.SCHEME_FILE_ID_ = b.ID_\n" + "		WHERE\n"
//				+ "			A.INVALID_ = 0\n AND A.CREATE_USER_=:username" + "	) T\n" + "WHERE\n" + "	1 = 1";
				+ "			A.INVALID_ = 0\n " + " order by A.CREATE_DATE_ desc	) T\n" + "WHERE\n" + "	1 = 1";
		Map<String, Object> params = new HashMap<String, Object>();
//		params.put("username", ContextHolder.getLoginUser().getUsername());
		ParseResult pr = this.parseCriteria(criteria, true, "T");
		if (pr != null) {
			StringBuffer sbSql = pr.getAssemblySql();
			sql += " and " + sbSql.toString();
			params.putAll(pr.getValueMap());
		}
		this.pagingQuery(page, sql, new RowMapper<SysEmergencyScheme>() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see
			 * org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet
			 * , int)
			 */
			@Override
			public SysEmergencyScheme mapRow(ResultSet rs, int rowNum) throws SQLException {
				SysEmergencyScheme a = new SysEmergencyScheme();
				a.setId(rs.getString("ID_"));
				a.setCreateUser(rs.getString("CREATE_USER_"));
				a.setCreateUserCname(rs.getString("CREATE_USER_CNAME_"));
				a.setCreateDate(rs.getDate("CREATE_DATE_"));
				a.setUpdateDate(rs.getDate("UPDATE_DATE_"));
				a.setUpdateUser(rs.getString("UPDATE_USER_"));
				a.setUpdateUserCname(rs.getString("UPDATE_USER_CNAME_"));
				a.setInvalid(rs.getBoolean("INVALID_"));
				a.setPurpose(rs.getString("PURPOSE_"));
				a.setSysId(rs.getString("SYS_ID_"));
				a.setSysName(rs.getString("SYS_NAME_"));
				a.setState(rs.getInt("STATE_"));
				a.setRange(rs.getInt("RANGE_"));
				a.setTitle(rs.getString("TITLE_"));
				a.setZdcto(rs.getString("ZDCTO_"));
				a.setZdctoName(rs.getString("ZDCTO_NAME_"));
				a.setSchemeFileId(rs.getString("SCHEME_FILE_ID_"));
				try {
					a = EntityUtils.toEntity(a);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				EntityUtils.setValue(a, "ywxqs", rs.getString("YWXQS_"));
				return a;
			}
		}, params);

	}

	public SysEmergencyScheme loadProject(String businessId) {
		String sql = "SELECT\n" + "	T.*\n" + "FROM\n" + "	(\n" + "		SELECT\n"
				+ "			A.*, B.FILE_NAME_ AS YWXQS_\n" + "		FROM\n" + "			T_SYS_EMERGENCY_SCHEME AS A\n"
				+ "		LEFT JOIN T_SYS_MATERIAL AS B ON a.SCHEME_FILE_ID_ = b.ID_\n" + "		WHERE\n"
				+ "		A.INVALID_=0 AND A.ID_=:id  " + "	) T\n" + "WHERE\n" + "	1 = 1";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", businessId);
		List<SysEmergencyScheme> ls = this.getNamedParameterJdbcTemplate().query(sql, params,
				new RowMapper<SysEmergencyScheme>() {
					/*
					 * (non-Javadoc)
					 * 
					 * @see
					 * org.springframework.jdbc.core.RowMapper#mapRow(java.sql
					 * .ResultSet , int)
					 */
					@Override
					public SysEmergencyScheme mapRow(ResultSet rs, int rowNum) throws SQLException {
						SysEmergencyScheme a = new SysEmergencyScheme();
						a.setId(rs.getString("ID_"));
						a.setCreateUser(rs.getString("CREATE_USER_"));
						a.setCreateDate(rs.getDate("CREATE_DATE_"));
						a.setUpdateDate(rs.getDate("UPDATE_DATE_"));
						a.setUpdateUser(rs.getString("UPDATE_USER_"));
						a.setInvalid(rs.getBoolean("INVALID_"));
						a.setPurpose(rs.getString("PURPOSE_"));
						a.setSysId(rs.getString("SYS_ID_"));
						a.setSysName(rs.getString("SYS_NAME_"));
						a.setState(rs.getInt("STATE_"));
						a.setRange(rs.getInt("RANGE_"));
						a.setTitle(rs.getString("TITLE_"));
						a.setZdcto(rs.getString("ZDCTO_"));
						a.setZdctoName(rs.getString("ZDCTO_NAME_"));
						a.setSchemeFileId(rs.getString("SCHEME_FILE_ID_"));
						try {
							a = EntityUtils.toEntity(a);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						EntityUtils.setValue(a, "ywxqs", rs.getString("YWXQS_"));
						return a;
					}
				});
		if (ls.size() > 0) {
			return ls.get(0);
		} else {
			return null;
		}

	}

}
